package com.neusoft.weixin.service;

import java.awt.FlowLayout;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.apache.http.NameValuePair;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class FormDemo {

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		// 登录和发表文章先不做讨论
		// login(httpclient);
		// postArticle(httpclient);
		// 注册
		regist(httpclient);
		// 关闭Httpclient
		httpclient.getConnectionManager().shutdown();

	}

	/**
	 * 注册
	 * 
	 * @param httpclient
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	private static void regist(DefaultHttpClient httpclient1)
			throws ClientProtocolException, IOException {
		/**
		 * 下载验证码
		 */
		// 验证码生成的大概原理就是随即生成一个验证码字符串（0-9，a-z），然
		// 后用io流和图片流将验证码写入图片中，最后将图片用servlet writer出
		// 通过get方式访问该servlet，得到验证码图片
		HttpGet httpget = new HttpGet("http://sp.drugadmin.com/showValidateCode?" + new Random().nextInt(1000));
		// 执行get请求

		DefaultHttpClient httpclient = new DefaultHttpClient();
		
		HttpResponse response = httpclient.execute(httpget);
		// 我们把返回的信息，保存在本地的一张图片里
		File file = new File("regist.jpg");
		OutputStream ops = new FileOutputStream(file);
		response.getEntity().writeTo(ops);
		ops.close();
		/**
		 * 这时我们可以弹出一个JFrame来显示刚刚保存的验证码，我现在做的是 验证码手动输入，当然还可以做程序来读验证码。
		 */
		JFrame frame = new JFrame();
		frame.setVisible(false);
		frame.setBounds(100, 100, 100, 100);
		frame.setLayout(new FlowLayout());
		ImageIcon icon = new ImageIcon("regist.jpg");
		frame.add(new JLabel(icon));
		frame.setVisible(true);
		/**
		 * 在控制台输入验证码
		 */
		System.out.println("输入你看到的验证码");
		Scanner scr = new Scanner(System.in);
		String code = "81231850072724283277";
		String captcha = scr.nextLine();
		EntityUtils.consume(response.getEntity());
		// 提交注册请求到regist.do,并以post方式提交一个表单，就是刚刚我们看到的注册信息
		HttpPost post = new HttpPost("http://sp.drugadmin.com/ivr/code/codeQuery.jhtml");
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("captchaUUID", ""));
		// 把控制台输入的验证码填入
		nvps.add(new BasicNameValuePair("code", code));
		nvps.add(new BasicNameValuePair("contactNo", ""));
		nvps.add(new BasicNameValuePair("systemId", "drug-web"));
		nvps.add(new BasicNameValuePair("backno", ""));
		nvps.add(new BasicNameValuePair("checkcode1", "8123"));
		nvps.add(new BasicNameValuePair("checkcode2", "1850"));
		nvps.add(new BasicNameValuePair("checkcode3", "0727"));
		nvps.add(new BasicNameValuePair("checkcode4", "2428"));
		nvps.add(new BasicNameValuePair("checkcode5", "3277"));
		nvps.add(new BasicNameValuePair("areaNo", "021"));
		nvps.add(new BasicNameValuePair("contactNo1", "11111111"));
		nvps.add(new BasicNameValuePair("captcha", captcha));
		nvps.add(new BasicNameValuePair("searchthrough", "3"));
		nvps.add(new BasicNameValuePair("Submit.x", "4"));
		nvps.add(new BasicNameValuePair("Submit.y", "10"));
		post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		response = httpclient.execute(post);
		System.out.println(response.getStatusLine());
		
		System.out.println(EntityUtils.toString(response.getEntity()));
		 
		
		EntityUtils.consume(response.getEntity());
		httpclient.getConnectionManager().shutdown();
	}

}
